package com.cmcc.qx.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.cmcc.qx.common.entity.Article;
import com.cmcc.qx.ws.controller.ArticleController;

public class ToHtmlUtil {
	
	private static final Logger logger = Logger.getLogger(ArticleController.class);
	
	/**
	 * 根据本地模板生成静态页面
	 * 
	 * @param JspFile
	 *            jsp路经
	 * @param HtmlFile
	 *            html路经
	 * @return
	 */
	public static boolean JspToHtmlFile(File f,Article art,List<Article> hotArts, List<String> tags, String filePath) {
		String str = "";
//		long beginDate = (new Date()).getTime();
		try {
			String tempStr = "";
			File file = new File(filePath);
			FileInputStream is = new FileInputStream(file);// 读取模块文件
			BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
			while ((tempStr = br.readLine()) != null)
				str = str + tempStr;
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		try {
			String hot = "";
			for(Article hotArt : hotArts){
				String hotStr = "<li><a target='_blank' href='"+hotArt.getId()+".html'>"+hotArt.getTitle()+"</a></li>";
				hot += hotStr;
			}
			String tagstr = "";
			for(String s : tags){
				String tag = "<a href='/tag/"+s+"/1/query.do' target='_blank'>"+s+"</a>";
				tagstr += tag;
			}
			str = str.replaceAll("#title#", new String(art.getTitle()));
			str = str.replaceAll("#text#", new String(art.getText()));
			str = str.replaceAll("#hotArt#", new String(hot));
			str = str.replaceAll("#tag#", new String(tagstr));
			str = str.replaceAll("#urlid#", new String(""+art.getId()));

			FileOutputStream fos = new FileOutputStream(f.getPath()); 
		    OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); 
		    osw.write(str); 
		    osw.flush(); 
//			System.out.println("共用时：" + ((new Date()).getTime() - beginDate)
//					+ "ms");
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	
	/**
	 * 生成导航头
	 * @param map
	 * @throws Exception
	 */
	public static void addHeadJsp(Map<String, String> map) throws Exception{
		String path = System.getProperty("webapp.root");
		path = path + "head.jsp";
		logger.info("path = "+path);
		File file = new File(path);
		if(file.exists()){
			file.delete();
		} 
			file.createNewFile();
			FileOutputStream fos = new FileOutputStream(file);
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"));
			bw.write("<%@ page language=\"java\" import=\"java.util.*\" pageEncoding=\"utf-8\"%>");
			bw.write("<div id=\"menu_area\"><div class=\"menuarea\">");
			for(Map.Entry<String, String> entry:map.entrySet()){
				if(entry.getValue().equals("首页")){
					bw.write("<div class=\"menutext\"><a href=\"<%=request.getContextPath() %>/main/" + entry.getKey() + "/1/query.do\" art=\""+ entry.getKey() +"\" class=\"menu_hover\" onclick=\"pageload(this)\">"+ entry.getValue() +"</a></div>");
					bw.write("<div class=\"menu_divider\"></div>");
				}else{
					bw.write("<div class=\"menutext\"><a href=\"<%=request.getContextPath() %>/main/"+ entry.getKey() +"/1/query.do\" art=\""+ entry.getKey() +"\" class=\"menu\" onclick=\"pageload(this)\">"+ entry.getValue() +"</a></div>");
					bw.write("<div class=\"menu_divider\"></div>");
				}
			}
			
			bw.write("<input id=\"artType\" value=\"1\" style=\"display: none\"/><div class=\"menu_divider\"></div></div></div>");
			bw.flush();
		
	}
	
}
